//
//  NEWSViewController.m
//  idoctor
//
//  Created by Mr Lee on 2017/6/1.
//  Copyright © 2017年 Mr Lee. All rights reserved.
//


#define newList @"http://www.tngou.net/api/info/list?"


#import "NEWSViewController.h"
#import "newsModel.h"
#import "newListableViewCell.h"
#import "NewsInfoViewController.h"

@interface NEWSViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSMutableDictionary *_listDict;

}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,assign)NSInteger page;

@end

@implementation NEWSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    
    [self initUI];
    
    [self loadData];
    
}

-(void)initData
{
    _listDict = [NSMutableDictionary dictionary];
    _dataArr = [NSMutableArray array];
    _page = 1;
}

-(void)initUI
{

    [self initTableView];
    
    
    
}

-(void)initTableView
{

    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"newListableViewCell" bundle:nil]
         forCellReuseIdentifier:@"newListableViewCell"];
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_dataArr removeAllObjects];
        
        [self loadNewsList:NO];
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        
        _page = 2;
        
    }];
    
    _tableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        _page++;
        
        [self loadNewsList:YES];

        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        
    }];

}

-(void)loadData
{
    
    [self loadNewsList:NO];

}

-(void)loadNewsList:(BOOL)more
{
    
    [_listDict setValue:@"2" forKey:@"id"];
    
    if (more) {//加载更多
        
     [_listDict setValue:[NSString stringWithFormat:@"%ld",(long)_page] forKey:@"page"];
        
    }else{
        
    }
    
    [SVProgressHUD showWithStatus:@"加载中"];
    
    
    [[HttpRequest shardWebUtil]getNetworkRequestURLString:newList parameters:_listDict success:^(id obj) {
        
        //DLog(@"字典----%@",_listDict);
        
//        DLog(@"成功的数据---%@",obj);
        
        BOOL status = obj[@"status"];
        if (status) {
            
            [SVProgressHUD dismiss];
            
            for (NSDictionary *dict in obj[@"tngou"]) {
                
                newsModel *model = [[newsModel alloc]initWithDictionary:dict error:nil];
                
                //DLog(@"单个模型----%@",model);
                
                [_dataArr addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [_tableView reloadData];
            });
            //DLog(@"模型的数量---%ld",_dataArr.count);
            
        }
        
    } fail:^(NSError *error) {
        
        DLog(@"失败的数据---%@,",error.userInfo);
        
    }];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return  _dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 97;

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    newListableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newListableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = _dataArr[indexPath.row];

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    newsModel *model = _dataArr[indexPath.row];
    
    NewsInfoViewController *webVc = [[NewsInfoViewController alloc]init];
    
    webVc.newsId = model.newsId;
    
    [self.navigationController pushViewController:webVc animated:YES];
    


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
